package routefinder;

import java.util.LinkedList;
import java.util.List;

public class City {

	/**
	 * The name of the city
	 */
	private String name;

	/**
	 * The neighbours of the city.
	 */
	private List<Link> neighbours;

	public City(String name) {
		if (name == null) {
			throw new NullPointerException("City name cannot be null.");
		}
		this.name = name;
		this.neighbours = new LinkedList<Link>();
	}

	public String getName() {
		return name;
	}

	public List<Link> getNeighbours() {
		return neighbours;
	}

	public void addNeighbour(City neighbour, int distance) {
		if (distance < 0) {
			throw new IllegalArgumentException(
					"Distance between two cities cannot be negative.");
		}
		neighbours.add(new Link(neighbour, distance));
		neighbour.neighbours.add(new Link(this, distance));
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final City other = (City) obj;
		return name.equals(other.getName());
	}

	@Override
	public String toString() {
		return name;
	}
}
